<?php

/**
 * @file
 * Definition of example_handler_custom_field
 */

/**
 * Provides a custom views field.
 */
class jessica_stylebook_handler_article_colours extends views_handler_field {
    function option_definition() {
        $options = parent::option_definition();
        return $options;
    }

    function options_form(&$form, &$form_state) {
        parent::options_form($form, $form_state);
    }

    function query() {
        // do nothing -- to override the parent query.
    }

    function render($data) {
        //dpm($data, 'data');
        $items = $data->field_field_styling_item;

        //dpm($items);

        $output = '';

        foreach ($items as $key => $item) {
            $fc_item = reset($item['rendered']['entity']['field_collection_item']);
            if (isset($fc_item['field_styling_item_id']) && !empty($fc_item['field_styling_item_id'])) {
                $item_entity = $fc_item['field_styling_item_id']['#items'][0]['entity'];
                if ($item_entity->status == 1) {
                    if (!empty($item_entity->field_item_colour['und'])) {
                        $colour_term = taxonomy_term_load($item_entity->field_item_colour['und'][0]['tid']);

                        if ($colour_term->field_hex_colour_code['und'][0]['safe_value'] == 'OTHER') {
                            $output .= '<div class="other-color"></div>';
                        }
                        else {
                            $output .= '<div style="background-color: ' . $colour_term->field_hex_colour_code['und'][0]['safe_value'] . '"></div>';
                        }
                    }
                }
            }
        }

        return $output;
    }
}

